package com.fanbai.score.common.Utils.log;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.ConcurrentLinkedQueue;


public class YD_Log {

	/** 输出log级别 */

	public static boolean isDebug = false;

	/** 线程队列 */
	private static ConcurrentLinkedQueue<YD_LogInfo> logQueue = new ConcurrentLinkedQueue<YD_LogInfo>();

	/**
	 * 获取消息队列
	 */
	public synchronized static ConcurrentLinkedQueue<YD_LogInfo> getLogQueue() {
		return logQueue;
	}

	/**
	 * 日志路径
	 */
	private static final String LOG_FILE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ydwl/";

	/**
	 * 记录日志信息
	 * @param content
	 * @param fileName
	 */
	private static synchronized void addLog(String content, String fileName) {
		logQueue.add(new YD_LogInfo(content, fileName));
		if (!YD_LogWriteThread.isWriteThreadLive) {
			new YD_LogWriteThread().start();
		}
	}

	/** 以级别为 i 的形式输出LOG */
	public static void i(String msg) {

		if (isDebug) {

			Log.i(Constant.LOG_YIJIA_DEFAULT, msg);
		}

	}

	/** 以级别为 i 的形式输出LOG */
	public static void i(String tag, String msg) {

		if (isDebug) {
			Log.i(tag, msg);
		}

	}

	/** 以级别为 e 的形式输出LOG */

	public static void e(String msg) {

		if (isDebug) {
			Log.e(Constant.LOG_YIJIA_DEFAULT, msg);
		}
	}

	/** 以级别为 e 的形式输出LOG */

	public static void e(String tag, String msg) {

		if (isDebug) {
			Log.e(tag, msg);
		}
	}

	/** 以级别为 e 的形式输出LOG */

	public static void e(String tag, String msg, Throwable throwable) {

		if (isDebug) {
			Log.e(tag, msg, throwable);
		}
	}

	public static void println(String msg) {
		if (isDebug) {
			System.out.println(msg);
		}
	}

	/**
	 * 重载的方法
	 * 
	 * @param msg
	 * @param tag
	 */
	public static void writeLog(String tag, String msg) {

		if (!isDebug) {
			return;
		}
		i(tag, msg);
		DateFormat formatter = new SimpleDateFormat("yyyyMMdd HH:mm:ss SSS", Locale.CHINESE);
		String time = formatter.format(new Date());
		String result = time + "->" + msg + "\r\n";// 日志文本
		addLog(result, tag);
	}

	/**
	 * 重载的方法
	 * 
	 * @param content
	 * @param name
	 */
	synchronized static void writeFile(String name, String content) {

		DateFormat formatter = new SimpleDateFormat("yyyyMMdd HH:mm:ss SSS", Locale.CHINESE);
		DateFormat dateformatter = new SimpleDateFormat("yyyyMMdd", Locale.CHINESE);
		try {
			String time = formatter.format(new Date());
			String result = time + "  " + content + "\r\n";// 日志文本
			String fileName = name + "_" + dateformatter.format(new Date()) + ".txt";
			String path = LOG_FILE_PATH;
			if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
				File dir = new File(path);
				if (!dir.exists()) {
					dir.mkdirs();
				}

				FileOutputStream fos = new FileOutputStream(path + "/" + fileName, true);
				fos.write(result.getBytes());
				fos.close();
			}
		} catch (Exception e) {
		}
	}

	/**
	 * 流程记录(文本日志+Log.i输出)
	 */
	public static void writeLog(String content) {
		if (!isDebug) {
			return;
		}
		writeLog(Constant.LOG_YIJIA_DEFAULT, content);
	}

	private class Constant {
		/** 输出Log名字常量 */
		private static final String LOG_YIJIA_DEFAULT = "ydwl_lock";// 默认TAG
	}
}
